//
//  PullDownView.m
//  SlideView
//
//  Created by zhaojh on 9/7/14.
//  Copyright (c) 2014年 zjh. All rights reserved.
//

#import "PullDownView.h"

#define kButtonTagInit 100

@interface PullDownView ()

@property (nonatomic) NSInteger selectButtonIndex;
@property (nonatomic, strong) UIImageView *lineImageView;

@property (nonatomic, strong) UIView *btnView;
@property (nonatomic, strong) UIImageView *arrawImageView;

@end

@implementation PullDownView

- (id)initWithFrame:(CGRect)frame withItems:(NSArray *)items
{
    self = [super initWithFrame:frame];
    if (self) {
        self.items = items;
        self.selectButtonIndex = kButtonTagInit;
        [self loadUI];
    }
    return self;
}

- (void)layoutSubviews
{
    CGFloat btnWidth = (self.frame.size.width)*1.0/self.items.count;
    CGRect btnViewFrame = self.frame;
    btnViewFrame.origin.y = 10;
    btnViewFrame.origin.x = 0;
    btnViewFrame.size.height -=10;
    self.btnView.frame = btnViewFrame;
    
    CGRect arrawFrame = CGRectMake((btnWidth-6)/2*(self.index+1) + (btnWidth)/2*(self.index), 0, 12, 11);
    self.arrawImageView.frame = arrawFrame;
    
    for (int i=0; i<self.items.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:i+kButtonTagInit];
        [button setFrame:CGRectMake(btnWidth*i, 0, btnWidth, self.btnView.frame.size.height)];
    }
    [self selectButton];
}

- (void)loadUI
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    self.btnView = [[UIView alloc] initWithFrame:self.frame];
    UIImage *bkImg = [UIImage imageNamed:@"pull_back_image"];
    self.btnView.backgroundColor = [UIColor colorWithPatternImage:bkImg];
    [self addSubview:self.btnView];
    
    self.arrawImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];
    self.arrawImageView.frame = CGRectZero;
    [self addSubview:self.arrawImageView];
    
    for (int i=0; i<self.items.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i+kButtonTagInit;
        [button addTarget:self action:@selector(buttonClick:)
         forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:self.items[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[PullDownView colorFromHexRGB:@"ffb751"] forState:UIControlStateNormal];
        [self.btnView addSubview:button];
    }
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    imageView.image = [UIImage imageNamed:@"downline"];
    self.lineImageView = imageView;
    [self.btnView addSubview:imageView];
}

- (void)buttonClick:(UIButton *)button
{
    self.selectButtonIndex = button.tag;
    [self selectButton];
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseButtonItem:)]) {
        [self.delegate chooseButtonItem:button.tag-kButtonTagInit];
    }
}

- (void)selectButton
{
    for (UIButton *btn in self.btnView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            if (btn.tag == self.selectButtonIndex) {
                btn.selected = YES;
                
                if (self.lineImageView) {
                    [UIView animateWithDuration:0.25 animations:^{
                        float width = 50;
                        CGRect imgFrame = btn.frame;
                        imgFrame.origin.y = self.btnView.frame.size.height-3;
                        imgFrame.origin.x += (imgFrame.size.width-width)/2;
                        imgFrame.size.width = width;
                        imgFrame.size.height = 3;
                        self.lineImageView.frame = imgFrame;
                    }];
                }
                
            } else {
                btn.selected = NO;
            }
        }
    }
}

//通过16进制计算颜色
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString
{
    UIColor *result = nil;
    unsigned int colorCode = 0;
    unsigned char redByte, greenByte, blueByte;
    
    if (nil != inColorString)
    {
        NSScanner *scanner = [NSScanner scannerWithString:inColorString];
        (void) [scanner scanHexInt:&colorCode]; // ignore error
    }
    redByte = (unsigned char) (colorCode >> 16);
    greenByte = (unsigned char) (colorCode >> 8);
    blueByte = (unsigned char) (colorCode); // masks off high bits
    result = [UIColor
              colorWithRed: (float)redByte / 0xff
              green: (float)greenByte/ 0xff
              blue: (float)blueByte / 0xff
              alpha:1.0];
    return result;
}

@end
